package com.pan.crawler.old;

import com.pan.entity.Chapter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 就爬这个网站了
 * www.mayiwxw.com
 */
public class Directory{

    private String bookName;

    private List<Chapter> chapterList;


    public void run(String url) {
        EdgeDriver edgeDriver = new EdgeDriver();
        edgeDriver.manage().window().minimize();//窗口设置最小化
        edgeDriver.navigate().to(url);
        edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));//隐藏式等待10秒
        WebElement infoDiv = edgeDriver.findElement(By.id("info"));
        this.bookName = infoDiv.findElement(By.cssSelector("h1")).getText();
        WebElement listDiv = edgeDriver.findElement(By.id("list"));
        chapterList = new ArrayList<>();
        int[] order = new int[]{0};
        this.chapterList =
        listDiv.findElements(By.cssSelector("a")).stream().skip(9)
                        .map(a->{
                            String title = a.getText();
                            String chapterUrl = a.getAttribute("href");
                            return new Chapter(order[0]++,title,chapterUrl);
                        }).collect(Collectors.toList());
        edgeDriver.quit();
    }

    public String getBookName() {
        return bookName;
    }

    public List<Chapter> getChapterList() {
        return chapterList;
    }
}
